<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
$info = pdo_get('yzzc_sun_goods', array('id' => $_GPC['id'], 'uniacid' => $_W['uniacid']));
if ($info['subscribe_duration']) {
    $info['subscribe_duration'] = explode(',', $info['subscribe_duration']);
    // $spec = pdo_getall('yzzc_sun_specprice',array('uniacid'=>$_W['uniacid'],'gid'=>$_GPC['id']),'','');
    $spec = pdo_fetchall('select * from ' . tablename('yzzc_sun_specprice') . " where uniacid =" . $_W['uniacid'] . " and gid = " . $_GPC['id'] . " order by spec asc");

}
$sku = pdo_getall('yzzc_sun_goods_sku',['goods_id'=>$info['id'],'is_del' => 0]);
if ($info['tag_id']) {
    $info['tag_id'] = explode(',', $info['tag_id']);
}else{
    $info['tag_id'] = [];
}

$nowshop = pdo_get('yzzc_sun_branch', array('id' => $info['sid']), array('id', 'name'));
$cartype = pdo_getall('yzzc_sun_cartype', array('uniacid' => $_W['uniacid']));
$car_tag = pdo_getall('yzzc_sun_car_tag', array('uniacid' => $_W['uniacid']));
$car_brand = pdo_getall('yzzc_sun_carbrand', array('uniacid' => $_W['uniacid']));
$shop = pdo_getall('yzzc_sun_branch', array('uniacid' => $_W['uniacid']), array('id', 'name'));
$nowcartype = pdo_get('yzzc_sun_cartype', array('id' => $info['cartype']), array('id', 'name'));

$store_info = [];
if ($info['store_id'] > 0){
    $store_info = pdo_get('yzzc_sun_store', array('store_id' => $info['store_id']));
}

if (checksubmit('submit')) {
    if ($_GPC['name'] == null) {
        message('请您车辆名称', '', 'error');
    }
    if (!isset($_GPC['carnum']) || !$_GPC['carnum']) {
        message('请您填写车牌号码', '', 'error');
    }
    if ($_GPC['pic'] == null) {
        message('请您写上传图片', '', 'error');
        die;
    }
    if ($_GPC['sid'] == null) {
        message('请您先添加门店', '', 'error');
        die;
    }
    $data = [
        'uniacid' => $_W['uniacid'],
        'sid' => $_GPC['sid'],
        'name' => $_GPC['name'],
        'structure' => $_GPC['structure'],
        'grarbox' => $_GPC['grarbox'],
        'depositway' => $_GPC['depositway'],
        'deposit' => $_GPC['deposit'],
        'money' => $_GPC['money'],
//        'cost_money' => $_GPC['cost_money'],
        'act_money' => $_GPC['act_money'],
        'cartype' => $_GPC['cartype'],
        'content' => $_GPC['content'],
        'fee' => $_GPC['fee'],
        'service_fee' => $_GPC['service_fee'],
        'hot' => $_GPC['hot'],
        'num' => $_GPC['num'],
        'pic' => $_GPC['pic'],
        'zx_service_fee' => $_GPC['zx_service_fee'],
        'brand_id' => $_GPC['brand_id'],
        'open_xz' => $_GPC['open_xz'],
        'platform_fee' =>$_GPC['platform_fee'],
        'createtime' => time()
    ];

    if (!empty($_GPC['tag_id'])) {
        $data['tag_id'] = implode(',', $_GPC['tag_id']);
    }

    if ($_GPC['subscribe_duration']) {
        $data['subscribe_duration'] = implode(',', $_GPC['subscribe_duration']);
    }


    if (empty($_GPC['id'])) {
        $res = pdo_insert('yzzc_sun_goods', $data, array('uniacid' => $_W['uniacid']));
        $gid = pdo_insertid();

        if ($res) {
            //添加sku
            foreach ($_GPC['carnum'] as $key => $value) {

                $sku = [
                    'carnum' => $value,
                    'colour' => $_GPC['colour'][$key],
                    'displacement' => $_GPC['displacement'][$key],
                    'goods_id' => $gid
                ];
                $sku = array_merge($sku, $data);
                pdo_insert('yzzc_sun_goods_sku', $sku);
            }

            if ($data['subscribe_duration']) {
                $newData = [];
                foreach ($_GPC['price'] as $k => $v) {
                    foreach ($_GPC['subscribe_duration'] as $kk => $vv) {
                        if ($k == $kk) {
                            $newData = [
                                'gid' => $gid,
                                'price' => $v,
                                'spec' => $vv,
                                'uniacid' => $_W['uniacid']
                            ];
                            if ($newData['price']) {
                                pdo_insert('yzzc_sun_specprice', $newData);
                                // message('添加成功',$this->createWebUrl('goods',array()),'success');
                            } else {
                                message('请输入对应活动价格！');
                            }

                        }
                    }
                }
            }

        }

    } else {
        $goods = pdo_get('yzzc_sun_goods',['id' => $_GPC['id'], 'uniacid' => $_W['uniacid']]);
//        if ($goods['source_type'] != 1 && $goods['sid'] != $_GPC['sid']){
//            //除平台以外。车辆不能跨平台添加
//            message('车辆不能修改们门店！');
//        }
        $res = pdo_update('yzzc_sun_goods', $data, array('id' => $_GPC['id'], 'uniacid' => $_W['uniacid']));
        foreach ($_GPC['sku_id'] as $key => $value) {
            if ($value){
                $sku = pdo_get('yzzc_sun_goods_sku',['id'=>$value]);
                $sku = [
                    'carnum' => $_GPC['carnum'][$key],
                    'colour' => $_GPC['colour'][$key],
                    'displacement' => $_GPC['displacement'][$key],
                ];
                $sku = array_merge($sku, $data);
                pdo_update('yzzc_sun_goods_sku', $sku,['id'=>$value, 'goods_id' => $_GPC['id']]);
            }else{
                $sku = [
                    'carnum' => $_GPC['carnum'][$key],
                    'colour' => $_GPC['colour'][$key],
                    'displacement' => $_GPC['displacement'][$key],
                    'goods_id' => $_GPC['id'],
                ];
                if ($goods['source_type'] != 1){
                    $sku['store_id'] = $goods['store_id'];
                    $sku['store_name'] = $goods['store_name'];
                    $sku['store_money'] = $goods['store_money'];
                    $sku['source_type'] = $goods['source_type'];
                }
                $sku = array_merge($sku, $data);
                pdo_insert('yzzc_sun_goods_sku', $sku);
            }
        }
        if ($res) {
            if ($data['subscribe_duration']) {
                pdo_delete('yzzc_sun_specprice', array('uniacid' => $_W['uniacid'], 'gid' => $_GPC['id']));
                $newData = [];
                foreach ($_GPC['price'] as $k => $v) {
                    foreach ($_GPC['subscribe_duration'] as $kk => $vv) {
                        if ($k == $kk) {
                            $newData = [
                                'gid' => $_GPC['id'],
                                'price' => $v,
                                'spec' => $vv,
                                'uniacid' => $_W['uniacid']
                            ];
                            if ($newData['price']) {
                                pdo_insert('yzzc_sun_specprice', $newData);
                            } else {
                                message('请输入对应活动价格！');
                            }

                        }
                    }
                }
            }
        }
    }
    if ($res) {
        message('成功', $this->createWebUrl('goods', array()), 'success');
    } else {
        message('失败', '', 'error');
    }
}

if ($_GPC['op'] == 'deleteSku'){
    $sku_id = $_GPC['sku_id'];
    if (!$sku_id){
        return_msg(-1,'数据错误');
    }

    $sku = pdo_get('yzzc_sun_goods_sku',['id'=>$sku_id]);
    if ($sku['is_del'] == 1){
        return_msg(-1,"车牌号：{$sku['carnum']}已删除");
    }
    $sku_count = pdo_count('yzzc_sun_goods_sku',['goods_id'=>$sku['goods_id'],'is_del'=>0]);
    if ($sku_count <= 1){
        return_msg(-1,"一个车型必须存在一个车牌号");
    }

    $res = pdo_update('yzzc_sun_goods_sku',['is_del' => 1],['id'=>$sku_id,'uniacid'=>$_W['uniacid']]);
    if ($res){
        return_msg(1,"车牌号：{$sku['carnum']} 删除成功");
    }else{
        return_msg(-1,"车牌号：{$sku['carnum']} 删除失败");
    }

}
include $this->template('web/goodsinfo');